﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace ClassLibrary
{
    public partial class WebUserController
    {
        public static WebUser Authenticate(string Username, string Password)
        {
            WebUser webuser = null;
            try
            {
                SqlCommand cmd = DBHandler.getCommand("sp_Login");
                DBHandler.AddParameter(ref cmd, "@Username", Username);
                DBHandler.AddParameter(ref cmd, "@password", Password);
                DataSet ds = DBHandler.ExecuteSP(cmd);

                webuser = new WebUser((Guid)ds.Tables[0].Rows[0]["Userid"], ds.Tables[0].Rows[0]["Username"].ToString());
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception WebUserController->Authenticate(): " + e, LogLevel.EXCEPTION);
            }

            return webuser;
        }

        public static WebUser GetWebUser(Guid UserId)
        {
            WebUser webuser = null;
            try
            {
                SqlCommand cmd = DBHandler.getCommand("sp_Get_WebUser");
                DBHandler.AddParameter(ref cmd, "@UserId", UserId);
                DataSet ds = DBHandler.ExecuteSP(cmd);
                webuser = new WebUser((Guid)ds.Tables[0].Rows[0]["Userid"], ds.Tables[0].Rows[0]["Username"].ToString());
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception WebUserController->GetWebUser(): " + e, LogLevel.EXCEPTION);
            }
            return webuser;
        }

        public static int CreateWebUser(string Username, string Password)
        {
            int result = -1;
            try
            {
                SqlCommand cmd = DBHandler.getCommand("sp_Insert_WebUser");
                DBHandler.AddParameter(ref cmd, "@Username", Username);
                DBHandler.AddParameter(ref cmd, "@Password", Password);
                result = DBHandler.ExecuteSPNonQuery(cmd);
            }
            catch (Exception e)
            {
                LogWriter.Print("Exception WebUserController->GetWebUser(): " + e, LogLevel.EXCEPTION);
            }
            return result;
        }

    }
}